<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : disabled ? $t('notice.view') : $t('update')"
             :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" :disabled="disabled"
             @keyup.enter="dataFormSubmitHandle()" label-width="100px">
      <el-form-item prop="name" label="类别">
        <el-input v-model="dataForm.name" placeholder="请填写"></el-input>
      </el-form-item>
      <el-form-item prop="scoreType" label="是否加分项">
        <el-select v-model="dataForm.scoreType">
          <el-option
              label="扣分项"
              :value="0"
          />
          <el-option
              label="加分项"
              :value="1"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="score" label="总分">
        <el-input v-model="dataForm.score" placeholder="请填写" onkeyup="value=value.replace(/[^\-\d]/g,'')"
                  min="0"/>
      </el-form-item>
<!--      <el-form-item prop="sort" label="排序">-->
      <!--        <el-input v-model="dataForm.sort" type="number" placeholder="请填写"-->
      <!--                  onkeyup="value=value.replace(/[^\-\d]/g,'')" min="0"/>-->
      <!--      </el-form-item>-->
    </el-form>
    <template v-if="!disabled" v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import {reactive, ref} from "vue";
import baseService from "@/service/baseService";
import {useI18n} from "vue-i18n";
import {ElMessage} from "element-plus";

const {t} = useI18n();
const emit = defineEmits(["refreshDataList"]);
const disabled = ref(false);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  name: "",
  templateId: "",
  id: "",
  score: "",
  sort: "",
  scoreType: 0
});

const rules = ref({
  name: [{required: true, message: t("validate.required"), trigger: "blur"}],
  score: [{required: true, message: t("validate.required"), trigger: "blur"}]
});

const init = (templateId, id, dis) => {
  dataForm.id = "";
  visible.value = true;
  dataForm.templateId = templateId;
  disabled.value = dis;

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    dataForm.id = id;
    getInfo(id);
  }
};

// 获取信息
const getInfo = (id) => {
  baseService.get(`/evaluation/clctemplatetype/${id}`).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/evaluation/clctemplatetype", dataForm).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init,
  dataForm
});
</script>
